<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_er_filedset_config extends discuz_table
{
	public function __construct() {

		$this->_table ='er_filedset_config';
		$this->_pk    = 'id';

		parent::__construct();
	}

	/**
	 * 获取系统配置
	 * @return array 系统数组
	 */
     public function fetch_all($ids = array(), $force_from_db = false) {
        return DB::fetch_all("SELECT * FROM %t", array($this->_table)); 	
     }

    /**
     * 根据key获取配置
     * @param string $key 配置键
     * @return array 配置数组
     */
    public function fetch_by_key($key) {
        return DB::fetch_first("SELECT * FROM %t WHERE `key`=%s", array($this->_table, $key));	
    }

    public function fetch_by_keys($keys) {
        $result = array();
        if (!empty($keys)) {
            $query = DB::query("SELECT * FROM %t WHERE `key` IN (%n)", array($this->_table, $keys));
            while ($row = DB::fetch($query)) {
                $result[$row['key']] = $row;
            }
        }
        return $result;
    }

    /**
     * 根据key更新配置
     * @param string $key 配置键
     * @param array $data 要更新的数据
     * @return bool 更新结果
     */
    public function update_by_key($key, $data) {

        if ($key == 'allowed_groups' && isset($data['value']) && is_array($data['value'])) {
            $data['value'] = serialize($data['value']);
        }

        if ($key == 'path_before' && isset($data['value']) && is_array($data['value'])) {
        $data['value'] = json_encode($data['value']);
    }

    	$result = DB::update($this->_table, $data, array('key' => $key));

        return [
            'success' => $result !== false,
            'message' => $result !== false ? '更新成功' : '更新失败',
            'code' => $result !== false ? 200 : 500
        ];
    }
}